﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace NJ.Common.Tools.Encrypt
{
    public class TripleDES
    {
        private static string key = "FGKC66QTZGPOXAGBTGMF5XPDOY7QQGX1";
        private static string iv = "12345678";
        private static CipherMode cipherMode = CipherMode.CBC;
        private static PaddingMode paddingMode = PaddingMode.PKCS7;

        #region 3des加密

        /// <summary>
        /// 3des加密
        /// </summary>
        /// <param name="aStrString"></param>
        /// <returns></returns>
        public static string Encrypt(string aStrString)
        {
            return Encrypt(aStrString, key);
        }

        /// <summary>
        /// 3des加密
        /// </summary>
        /// <param name="aStrString"></param>
        /// <param name="aStrKey"></param>
        /// <returns></returns>
        public static string Encrypt(string aStrString, string aStrKey)
        {
            return Encrypt(aStrString, aStrKey, cipherMode, paddingMode, iv);
        }

        /// <summary>
        /// 3des加密
        /// </summary>
        /// <param name="aStrString"></param>
        /// <param name="aStrKey"></param>
        /// <param name="mode"></param>
        /// <param name="pkmodel"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Encrypt(string aStrString, string aStrKey, CipherMode mode, PaddingMode pkmodel, string iv)
        {
            try
            {
                aStrKey = aStrKey.Substring(0, 24);
                var des = new TripleDESCryptoServiceProvider
                {
                    Key = Encoding.UTF8.GetBytes(aStrKey),
                    Mode = mode,
                    Padding = pkmodel
                };
                if (mode == CipherMode.CBC)
                {
                    des.IV = Encoding.UTF8.GetBytes(iv);
                }
                var desEncrypt = des.CreateEncryptor();
                byte[] buffer = Encoding.UTF8.GetBytes(aStrString);
                return Convert.ToBase64String(desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch
            {
                return string.Empty;
            }
        }

        #endregion 3des加密

        #region 3des解密

        /// <summary>
        /// 3des解密
        /// </summary>
        /// <param name="aStrString"></param>
        /// <returns></returns>
        public static string Decrypt(string aStrString)
        {
            return Decrypt(aStrString, key);
        }

        /// <summary>
        /// 3des解密
        /// </summary>
        /// <param name="aStrString"></param>
        /// <param name="aStrKey"></param>
        /// <returns></returns>
        public static string Decrypt(string aStrString, string aStrKey)
        {
            return Decrypt(aStrString, aStrKey, cipherMode, paddingMode, iv);
        }

        /// <summary>
        /// 3des解密
        /// </summary>
        /// <param name="aStrString"></param>
        /// <param name="aStrKey"></param>
        /// <param name="mode"></param>
        /// <param name="pkmodel"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Decrypt(string aStrString, string aStrKey, CipherMode mode, PaddingMode pkmodel, string iv)
        {
            try
            {
                aStrKey = aStrKey.Substring(0, 24);
                var des = new TripleDESCryptoServiceProvider
                {
                    Key = Encoding.UTF8.GetBytes(aStrKey),
                    Mode = mode,
                    Padding = pkmodel
                };
                if (mode == CipherMode.CBC)
                {
                    des.IV = Encoding.UTF8.GetBytes(iv);
                }
                var desDecrypt = des.CreateDecryptor();
                var result = "";
                byte[] buffer = Convert.FromBase64String(aStrString);
                result = Encoding.UTF8.GetString(desDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
                return result;
            }
            catch
            {
                return string.Empty;
            }
        }

        #endregion 3des解密
    }
}